﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.Pop.usControl
{
    /// <summary>
    /// 
    /// </summary>
    public partial class RepairCase : System.Web.UI.UserControl, System.Web.UI.IPostBackEventHandler
    {
        /// <summary>
        /// Gets or sets the case status.
        /// </summary>
        /// <value>
        /// The case status.
        /// </value>
        public string RepairCaseStatus
        {
            get { return (ViewState["RepairCaseStatus"] == null) ? "" : ViewState["RepairCaseStatus"].ToString(); }
            set { ViewState["RepairCaseStatus"] = value; }
        }
        /// <summary>
        /// Gets or sets the consulting case ID.
        /// </summary>
        /// <value>
        /// The consulting case ID.
        /// </value>
        public string RepairCaseID
        {
            get { return (ViewState["RepairCaseID"] == null) ? "" : ViewState["RepairCaseID"].ToString(); }
            set { ViewState["RepairCaseID"] = value; }
        }
        /// <summary>
        /// 客户信息 Place Holder 容器
        /// Gets or sets the place holder customer info.
        /// </summary>
        /// <value>
        /// The place holder customer info.
        /// </value>
        public PlaceHolder PlaceHolderCustomerInfo { get; set; }
        /// <summary>
        /// 父页面
        /// Gets or sets the parent page.
        /// </summary>
        /// <value>
        /// The parent page.
        /// </value>
        public Page ParentPage { get; set; }
        /// <summary>
        /// 用户登录信息
        /// Gets or sets the user login info.
        /// </summary>
        /// <value>
        /// The user login info.
        /// </value>
        public UserLoginInfo UserLoginInfo { get; set; }
        /// <summary>
        /// Gets or sets the sale vehicle entity.
        /// </summary>
        /// <value>
        /// The sale vehicle entity.
        /// </value>
        public T_SALEVEHICLE_Entity SaleVehicleEntity { get; set; }



        #region 对象访问业务类
        /// <summary>
        /// 客户信息
        /// </summary>
        T_CUSTOMER_BLL customerBll = new T_CUSTOMER_BLL();
        /// <summary>
        /// 报修工单
        /// </summary>
        private T_REPAIRCASE_BLL repairCaseBll = new T_REPAIRCASE_BLL();
        /// <summary>
        /// 
        /// </summary>
        private T_WORKORDEROPERATION_BLL workOrderOperationBll = new T_WORKORDEROPERATION_BLL();
        /// <summary>
        /// 数据字典
        /// </summary>
        T_DICTIONARY_BLL dictionaryBll = new T_DICTIONARY_BLL();
        /// <summary>
        /// 工单状态
        /// </summary>
        T_WORKORDERSTATUS_BLL workOrderStatusBll = new T_WORKORDERSTATUS_BLL();
        /// <summary>
        /// 事业部
        /// </summary>
        T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL();
        /// <summary>
        /// 市场部
        /// </summary>
        T_MARKETDEPARTMENT_BLL marketDepartmentBll = new T_MARKETDEPARTMENT_BLL();
        /// <summary>
        /// 经销商
        /// </summary>
        T_DEALER_BLL dealerBll = new T_DEALER_BLL();
        /// <summary>
        /// 服务部
        /// </summary>
        T_SERVICEPROVIDER_BLL serviceProviderBll = new T_SERVICEPROVIDER_BLL();
        /// <summary>
        /// 供应商
        /// </summary>
        T_SUPPLIER_BLL supplierBll = new T_SUPPLIER_BLL();
        /// <summary>
        /// 中心部门
        /// </summary>
        T_DEPARTMENT_BLL departmentbll = new T_DEPARTMENT_BLL();
        /// <summary>
        /// 操作员
        /// </summary>
        T_USER_BLL userBll = new T_USER_BLL();
        /// <summary>
        /// 配置
        /// </summary>
        private T_CONFIG_BLL configBll = new T_CONFIG_BLL();
        /// <summary>
        ///  工单操作日志
        /// </summary>
        private T_WORKORDERLOG_BLL workOrderLogBll = new T_WORKORDERLOG_BLL();

        private T_FAULTSYMPTOM_BLL faultSymptomBll = new T_FAULTSYMPTOM_BLL();
        /// <summary>
        /// 录音
        /// </summary>
        private T_RECORD_BLL recordBll = new T_RECORD_BLL();
        /// <summary>
        /// 
        /// </summary>
        T_LOCATIONINFORMATION_BLL liBll = new T_LOCATIONINFORMATION_BLL();

        /// <summary>
        /// 
        /// </summary>
        T_CUSTOMERTELINDEX_BLL ctBll = new T_CUSTOMERTELINDEX_BLL();
        #endregion

        public void RaisePostBackEvent(string eventArgument)
        {
            Button btn = phButton.FindControl(eventArgument) as Button;
            if (btn != null)
            {
                btnOK_Click(btn, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "RepairCase_Page_LoadBegin");
            PageHelper.SetPopCalender(txtPurchaseDate, "yyyy-MM-dd");
            if(!IsPostBack)
            {
                
                ddlRepairModeBinding();
                ddlRepairTypeBinding();

                ddlDEALERCODEBinding();
                ddlSTATIONCODEBinding();
                ddlDISPATCHINGDELAYBinding();
                ddlDispatchingTypeBinding();

                ddlProcessingObjectTypeBinding();
                ddlProcessingObjectType.SelectedValue = ((int)UserType.供应商).ToString();
                ddlProcessingDepartmentIDBinding();
                ddlProcessingUserIDBinding();

                ddlHANDLINGBinding();
                ddlFAILURELEVELBinding();

                ddlREMINDERTIMEBinding();

                ddlVCOUNTRYBinding();
                ddlVCOUNTRY_SelectedIndexChanged(null, null);

                try
                {
                    ddlREMINDERTIME.SelectedValue = configBll.GetCacheValueByName(this.Page,
                                                                                  "RepairDefaultReminderTime");
                }
                catch (Exception ex)
                {
                }

                pageInit();



            }
            LoadButton();
            UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "RepairCase_Page_LoadEnd");

        }

        /// <summary>
        /// 
        /// Loads the button.
        /// </summary>
        public void LoadButton()
        {
            phButton.Controls.Clear();
            if (String.IsNullOrEmpty(lblRepairCaseID.Text.Trim()))
            {
                Button btn = new Button();
                btn.ID = StaticConstant.PreRepairCase + "SAVE";
                btn.CommandName = StaticConstant.PreRepairCase + "SAVE";
                btn.Text = "保 存";
                btn.CssClass = "btn";
                //if (entity.ISCONFIRM.ToString() == ((int)IsConfirm.是).ToString())
                //{
                //    btn.OnClientClick = String.Format("return confirm('您确认要执行 {0} 操作吗？');", entity.OPERATIONNAME);
                //}
                //btn.Click += new EventHandler(btnOK_Click);
                StringBuilder sb = new StringBuilder();
                sb.Append("if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate() == false) { return false; }};");
                sb.Append("this.disabled  = true;");
                sb.Append(this.Page.ClientScript.GetPostBackEventReference(this, btn.ID.ToString()));
                sb.Append(";");
                btn.Attributes.Add("onclick", sb.ToString());
                //btn.OnClientClick = "this.disabled=true;this.form.submit();";
                //btn.UseSubmitBehavior = false;
                phButton.Controls.Add(btn);
            }
            else
            {
                List<T_WORKORDEROPERATION_Entity> workOrderOperationList =
                    workOrderOperationBll.GetList(
                        string.Format(" and CurrentStatusID = '{0}' and OrderTypeID = '{1}'  and IsPopPage = '{2}'  ", RepairCaseStatus,
                                      ((int)WorkOrderType.报修工单).ToString(), ((int)IsPopPage.是).ToString()), " SortOrder desc, OperationID asc ");
                foreach (var entity in workOrderOperationList)
                {
                    Button btn = new Button();
                    btn.ID = StaticConstant.PreRepairCase + entity.OPERATIONID.ToString();
                    btn.CommandName = StaticConstant.PreRepairCase + entity.OPERATIONID.ToString();
                    btn.Text = entity.OPERATIONNAME;
                    btn.CssClass = "btn";
                    if (entity.ISCONFIRM.ToString() == ((int)IsConfirm.是).ToString())
                    {
                        btn.OnClientClick = String.Format("return confirm('您确认要执行 {0} 操作吗？');", entity.OPERATIONNAME);
                    }
                    btn.Click += new EventHandler(btnOK_Click);
                    phButton.Controls.Add(btn);
                }
            }
        }

        public void pageInit()
        {
            if(repairCaseBll.CheckSymptomReadOnly(UserLoginInfo))
            {
                txtSYMPTOM.ReadOnly = true;
                txtFAILUREPARTS.ReadOnly = true;
                txtFULLSYMPTOM.ReadOnly = true;
            }
            if (String.IsNullOrEmpty(lblRepairCaseID.Text.Trim()))
            {
                RepairCaseStatus = "";
                if (SaleVehicleEntity != null)
                {
                    txtPRODUCTCODE.Text = SaleVehicleEntity.PRODUCTCODE;
                    txtBRAND.Text = SaleVehicleEntity.BRAND;
                    txtVEHICLETYPE.Text = SaleVehicleEntity.VEHICLETYPE;
                    txtSERIES.Text = SaleVehicleEntity.SERIES;
                    txtMODELS.Text = SaleVehicleEntity.MODELS;
                    txtENGINEMODEL.Text = SaleVehicleEntity.ENGINEMODEL;
                    txtENGINEMANUFACTURERS.Text = SaleVehicleEntity.ENGINEMANUFACTURERS;
                    txtENGINENUMBER.Text = SaleVehicleEntity.ENGINENUMBER;
                    txtOWNERSHOMEPHONE.Text = SaleVehicleEntity.OWNERSHOMEPHONE;
                    txtOWNERSMOBILEPHONE.Text = SaleVehicleEntity.OWNERSMOBILEPHONE;
                    txtOWNERSPEOPLE.Text = SaleVehicleEntity.CUSTOMERNAME;
                    txtOWNERSPEOPLECODE.Text = SaleVehicleEntity.CUSTOMERCODE;
                    ddlSTATIONCODE.SelectedValue = SaleVehicleEntity.STATIONCODE;
                    ddlDEALERCODE.SelectedValue = SaleVehicleEntity.DEALERCODE;

                }
                else
                {
                    PageHelper.ClearControlValue(this);
                    ddlProcessingObjectType.SelectedValue = ((int)UserType.服务商).ToString();
                    ddlPROCESSINGMARKCODEBinding();
                    ddlProcessingDepartmentIDBinding();
                    ddlProcessingUserIDBinding();
                    try
                    {
                        ddlREMINDERTIME.SelectedValue = configBll.GetCacheValueByName(this.Page,
                                                                                      "RepairDefaultReminderTime");
                    }
                    catch(Exception ex)
                    {
                    }
                }
            }
            else
            {

            }

            switch (UserLoginInfo.GetCurrentUserType())
            {
                case UserType.中心部门:
                    {

                    }
                    break;
                case UserType.市场部:
                    {

                    }
                    break;
                case UserType.供应商:
                    {

                    }
                    break;
                case UserType.经销商:
                    {
                        ddlProcessingObjectType.SelectedValue = ((int)UserType.经销商).ToString();
                        ddlPROCESSINGMARKCODEBinding();
                        ddlPROCESSINGMARKCODE.SelectedValue = UserLoginInfo.ServiceProviderEntity.MARKETDEPARTMENTCODE;
                        ddlProcessingDepartmentIDBinding();
                        ddlProcessingDepartmentID.SelectedValue = UserLoginInfo.ServiceProviderEntity.SERVICEPROVIDERID.ToString();
                        ddlProcessingObjectType.Enabled = false;
                        ddlPROCESSINGMARKCODE.Enabled = false;
                        ddlProcessingDepartmentID.Enabled = false;
                    }
                    break;
                case UserType.服务商:
                    {
                        ddlProcessingObjectType.SelectedValue = ((int)UserType.服务商).ToString();
                        ddlPROCESSINGMARKCODEBinding();
                        ddlPROCESSINGMARKCODE.SelectedValue = UserLoginInfo.ServiceProviderEntity.MARKETDEPARTMENTCODE;
                        ddlProcessingDepartmentIDBinding();
                        ddlProcessingDepartmentID.SelectedValue = UserLoginInfo.ServiceProviderEntity.SERVICEPROVIDERID.ToString();
                        ddlProcessingObjectType.Enabled = false;
                        ddlPROCESSINGMARKCODE.Enabled = false;
                        ddlProcessingDepartmentID.Enabled = false;
                    }
                    break;
            }
        }


        #region DDL  事件

        

        protected void ddlVCOUNTRY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVPROVINCEBinding();
            ddlVPROVINCE_SelectedIndexChanged(null, null);
            txtFAILUREOCCURS.Text = ddlVCOUNTRY.SelectedItem.Text;
        }

        protected void ddlVPROVINCE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVPREFECTURECITYBinding();
            ddlVPREFECTURECITY_SelectedIndexChanged(null, null);
            txtFAILUREOCCURS.Text = ddlVCOUNTRY.SelectedItem.Text;
            txtFAILUREOCCURS.Text +=  ddlVPROVINCE.SelectedItem.Text;
        }

        protected void ddlVPREFECTURECITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVCITYBinding();
            ddlVCITY_SelectedIndexChanged(null, null);
            txtFAILUREOCCURS.Text = ddlVCOUNTRY.SelectedItem.Text;
            txtFAILUREOCCURS.Text +=  ddlVPROVINCE.SelectedItem.Text;
            txtFAILUREOCCURS.Text +=  ddlVPREFECTURECITY.SelectedItem.Text;
        }

        protected void ddlVCITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVVILLAGETOWNBinding();
            txtFAILUREOCCURS.Text = ddlVCOUNTRY.SelectedItem.Text;
            txtFAILUREOCCURS.Text +=  ddlVPROVINCE.SelectedItem.Text;
            txtFAILUREOCCURS.Text +=  ddlVPREFECTURECITY.SelectedItem.Text;
            txtFAILUREOCCURS.Text +=  ddlVCITY.SelectedItem.Text;
        }

        #endregion


        #region MyRegion 

        /// <summary>
        /// DDLs the DISPATCHINGDELAY binding.
        /// </summary>
        private void ddlDISPATCHINGDELAYBinding()
        {
            ddlDISPATCHINGDELAY.Items.Clear();
            EnumHelper.FillListControl(ddlDISPATCHINGDELAY, typeof(DispatchingDelay));
        }
        
        /// <summary>
        /// DDLs the repair type binding.
        /// </summary>
        private void ddlRepairTypeBinding()
        {

            ddlRepairType.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                            DictionaryType.GetNameByKey("RepairType"));
            ddlRepairType.DataTextField = "DictValue";
            ddlRepairType.DataValueField = "DictKey";
            ddlRepairType.DataBind();
            ddlRepairType.Items.Insert(0, new ListItem("", ""));

            try
            {
                ddlRepairType.SelectedValue = ddlRepairType.Items.FindByText("保内维修").Value;

            }
            catch { }

        }

        /// <summary>
        /// DDLs the repair mode binding.
        /// </summary>
        private void ddlRepairModeBinding()
        {

            ddlRepairMode.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                            DictionaryType.GetNameByKey("RepairMode"));
            ddlRepairMode.DataTextField = "DictValue";
            ddlRepairMode.DataValueField = "DictKey";
            ddlRepairMode.DataBind();
            ddlRepairMode.Items.Insert(0, new ListItem("", ""));

            try
            {
                ddlRepairMode.SelectedValue = ddlRepairMode.Items.FindByText("正常报单").Value;

            }
            catch { }
        }


        #region 国家省份城市

        /// <summary>
        /// DDLs the COUNTRY binding.
        /// </summary>
        private void ddlVCOUNTRYBinding()
        {

            ddlVCOUNTRY.DataSource = liBll.GetCacheListByParentCode(this.Page, "");
            ddlVCOUNTRY.DataTextField = "NAME";
            ddlVCOUNTRY.DataValueField = "CODE";
            ddlVCOUNTRY.DataBind();
            ddlVCOUNTRY.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the PROVINCE binding.
        /// </summary>
        private void ddlVPROVINCEBinding()
        {
            ddlVPROVINCE.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlVCOUNTRY.SelectedValue.Trim());
            ddlVPROVINCE.DataTextField = "NAME";
            ddlVPROVINCE.DataValueField = "CODE";
            ddlVPROVINCE.DataBind();
            ddlVPROVINCE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the PREFECTURECITY binding.
        /// </summary>
        private void ddlVPREFECTURECITYBinding()
        {
            ddlVPREFECTURECITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlVPROVINCE.SelectedValue.Trim());
            ddlVPREFECTURECITY.DataTextField = "NAME";
            ddlVPREFECTURECITY.DataValueField = "CODE";
            ddlVPREFECTURECITY.DataBind();
            ddlVPREFECTURECITY.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the CITY binding.
        /// </summary>
        private void ddlVCITYBinding()
        {
            ddlVCITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlVPREFECTURECITY.SelectedValue.Trim());
            ddlVCITY.DataTextField = "NAME";
            ddlVCITY.DataValueField = "CODE";
            ddlVCITY.DataBind();
            ddlVCITY.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the VILLAGETOWN binding.
        /// </summary>
        private void ddlVVILLAGETOWNBinding()
        {
            //ddlVVILLAGETOWN.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlVCITY.SelectedValue.Trim());
            //ddlVVILLAGETOWN.DataTextField = "NAME";
            //ddlVVILLAGETOWN.DataValueField = "CODE";
            //ddlVVILLAGETOWN.DataBind();
            //ddlVVILLAGETOWN.Items.Insert(0, new ListItem("", ""));
        } 
        #endregion

        private void ddlSTATIONCODEBinding()
        {
            string businessDivisionCode = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            var list = serviceProviderBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode), " SERVICEPROVIDERNAME asc ")
                           .Select(p => new
                           {
                               ServiceProviderName = p.SERVICEPROVIDERNAME,
                               ServiceProviderID = p.SERVICEPROVIDERID,
                               ServiceProviderCode = p.SERVICEPROVIDERCODE
                           });
            ddlSTATIONCODE.DataSource = list;
            ddlSTATIONCODE.DataTextField = "ServiceProviderName";
            ddlSTATIONCODE.DataValueField = "ServiceProviderCode";
            ddlSTATIONCODE.DataBind();
            ddlSTATIONCODE.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlDEALERCODEBinding()
        {
            string businessDivisionCode = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            var list = dealerBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode), " DEALERNAME asc ")
                           .Select(p => new
                           {
                               DealerName =  p.DEALERNAME,
                               DealerID = p.DEALERID,
                               DealerCode = p.DEALERCODE
                           });
            ddlDEALERCODE.DataSource = list;
            ddlDEALERCODE.DataTextField = "DealerName";
            ddlDEALERCODE.DataValueField = "DealerCode";
            ddlDEALERCODE.DataBind();

            ddlDEALERCODE.Items.Insert(0, new ListItem("", ""));
        }
        
        /// <summary>
        /// 闭环时间
        /// DDLs the REMINDERTIME binding.
        /// </summary>
        private void ddlREMINDERTIMEBinding()
        {

            ddlREMINDERTIME.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                            DictionaryType.GetNameByKey("RepairReminderTime"));
            ddlREMINDERTIME.DataTextField = "DictValue";
            ddlREMINDERTIME.DataValueField = "DictKey";
            ddlREMINDERTIME.DataBind();
            ddlREMINDERTIME.Items.Insert(0, new ListItem("无", ""));
        }

        /// <summary>
        /// 故障等级
        /// DDLs the FAILURELEVEL binding.
        /// </summary>
        private void ddlFAILURELEVELBinding()
        {
            ddlFAILURELEVEL.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("FailureLevel"));
            ddlFAILURELEVEL.DataTextField = "DictValue";
            ddlFAILURELEVEL.DataValueField = "DictKey";
            ddlFAILURELEVEL.DataBind();
            ddlFAILURELEVEL.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 处理方式
        /// DDLs the HANDLING binding.
        /// </summary>
        private void ddlHANDLINGBinding()
        {
            //ddlHANDLING.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("RepairHandling"));
            //ddlHANDLING.DataTextField = "DictValue";
            //ddlHANDLING.DataValueField = "DictKey";
            //ddlHANDLING.DataBind();
            //ddlHANDLING.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 派工方式
        /// DDLs the dispatching type binding.
        /// </summary>
        private void ddlDispatchingTypeBinding()
        {
            ddlDispatchingType.Items.Clear();
            EnumHelper.FillListControl(ddlDispatchingType, typeof(DispatchingType));
        }


        /// <summary>
        /// 派工对象
        /// DDLs the processing object type binding.
        /// </summary>
        private void ddlProcessingObjectTypeBinding()
        {
            ddlProcessingObjectType.Items.Clear();
            EnumHelper.FillListControl(ddlProcessingObjectType, typeof(UserType));
        }

        private void ddlPROCESSINGMARKCODEBinding()
        {
            string businessDivisionCode = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            ddlPROCESSINGMARKCODE.Items.Clear();
            switch ((UserType)Enum.Parse(typeof(UserType), ddlProcessingObjectType.SelectedValue))
            {
                case UserType.中心部门:
                case UserType.供应商:
                case UserType.市场部:
                    {
                        ddlPROCESSINGMARKCODE.Visible = false;
                    }
                    break;
                case UserType.服务商:
                case UserType.经销商:
                    {
                        ddlPROCESSINGMARKCODE.Visible = true;
                        var list = marketDepartmentBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode), " MarketDepartmentNAME asc ")
                            .Select(p => new
                            {
                                Name = (p.MARKETDEPARTMENTNAME.Length > 0) ? StringHelper.GetChineseSpell(p.MARKETDEPARTMENTNAME)[0].ToString() + "-" + p.MARKETDEPARTMENTNAME : p.MARKETDEPARTMENTNAME,
                                ID = p.MARKETDEPARTMENTID,
                                Code = p.MARKETDEPARTMENTCODE
                            });
                        ddlPROCESSINGMARKCODE.DataSource = list;
                        ddlPROCESSINGMARKCODE.DataTextField = "Name";
                        ddlPROCESSINGMARKCODE.DataValueField = "Code";
                        ddlPROCESSINGMARKCODE.DataBind();
                    }
                    break;
                default:
                    {

                    }
                    break;
            }
            ddlPROCESSINGMARKCODE.Items.Insert(0, new ListItem("", ""));
        }


        /// <summary>
        /// 派工部门
        /// DDLs the processing department ID binding.
        /// </summary>
        private void ddlProcessingDepartmentIDBinding()
        {
            string businessDivisionCode = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;

            switch ((UserType)Enum.Parse(typeof(UserType), ddlProcessingObjectType.SelectedValue))
            {
                case UserType.中心部门:
                    {
                        departmentbll.FillListControl(ddlProcessingDepartmentID, "0", businessDivisionCode, ((int)DepartmentType.服务部门).ToString());
                        ddlProcessingDepartmentIDTwo.DataSource = departmentbll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and IsCenter = '{1}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode, ((int)DepartmentType.服务部门).ToString()));
                        ddlProcessingDepartmentIDTwo.DataTextField = "DepartmentCode";
                        ddlProcessingDepartmentIDTwo.DataValueField = "DepartmentID";
                        ddlProcessingDepartmentIDTwo.DataBind();
                    }
                    break;
                case UserType.供应商:
                    {
                        var list =
                            supplierBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode), " SUPPLIERNAME asc ")
                                .Select(p =>new
                                        {
                                            SupplierName = (p.SUPPLIERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.SUPPLIERNAME)[0].ToString() + "-" + p.SUPPLIERNAME : p.SUPPLIERNAME,
                                            SupplierID = p.SUPPLIERID,
                                            SupplierCode = p.SUPPLIERCODE
                                        });
                        ddlProcessingDepartmentID.DataSource = list;
                        ddlProcessingDepartmentID.DataTextField = "SupplierName";
                        ddlProcessingDepartmentID.DataValueField = "SupplierID";
                        ddlProcessingDepartmentID.DataBind();
                        ddlProcessingDepartmentIDTwo.DataSource = list;
                        ddlProcessingDepartmentIDTwo.DataTextField = "SupplierCode";
                        ddlProcessingDepartmentIDTwo.DataValueField = "SupplierID";
                        ddlProcessingDepartmentIDTwo.DataBind();

                    }
                    break;
                case UserType.市场部:
                    {
                        var list = marketDepartmentBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode), " MARKETDEPARTMENTNAME asc ")
                            .Select(p=> new
                                            {
                                                MarketDepartmentName = (p.MARKETDEPARTMENTNAME.Length > 0) ? StringHelper.GetChineseSpell(p.MARKETDEPARTMENTNAME)[0].ToString() + "-" + p.MARKETDEPARTMENTNAME : p.MARKETDEPARTMENTNAME,
                                                MarketDepartmentID = p.MARKETDEPARTMENTID,
                                                MarketDepartmentCode = p.MARKETDEPARTMENTCODE
                                            });
                        ddlProcessingDepartmentID.DataSource = list;
                        ddlProcessingDepartmentID.DataTextField = "MarketDepartmentName";
                        ddlProcessingDepartmentID.DataValueField = "MarketDepartmentID";
                        ddlProcessingDepartmentID.DataBind();
                        ddlProcessingDepartmentIDTwo.DataSource = list;
                        ddlProcessingDepartmentIDTwo.DataTextField = "MarketDepartmentCode";
                        ddlProcessingDepartmentIDTwo.DataValueField = "MarketDepartmentID";
                        ddlProcessingDepartmentIDTwo.DataBind();
                    }
                    break;
                case UserType.服务商:
                    {
                        var list = serviceProviderBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and  MARKETDEPARTMENTCODE = '{1}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode, ddlPROCESSINGMARKCODE.SelectedValue.Trim()), " SERVICEPROVIDERNAME asc ")
                            .Select(p=>new
                                           {
                                               ServiceProviderName = (p.SERVICEPROVIDERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.SERVICEPROVIDERNAME)[0].ToString() + "-" + p.SERVICEPROVIDERNAME : p.SERVICEPROVIDERNAME,
                                               ServiceProviderID = p.SERVICEPROVIDERID,
                                               ServiceProviderCode = p.SERVICEPROVIDERCODE
                                           });
                        ddlProcessingDepartmentID.DataSource = list;
                        ddlProcessingDepartmentID.DataTextField = "ServiceProviderName";
                        ddlProcessingDepartmentID.DataValueField = "ServiceProviderID";
                        ddlProcessingDepartmentID.DataBind();
                        ddlProcessingDepartmentIDTwo.DataSource = list;
                        ddlProcessingDepartmentIDTwo.DataTextField = "ServiceProviderCode";
                        ddlProcessingDepartmentIDTwo.DataValueField = "ServiceProviderID";
                        ddlProcessingDepartmentIDTwo.DataBind();
                    }
                    break;
                case UserType.经销商:
                    {
                        var list = dealerBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and  MARKETDEPARTMENTCODE = '{1}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode, ddlPROCESSINGMARKCODE.SelectedValue.Trim()), " DEALERNAME asc ")
                            .Select(p=>new
                                           {
                                               DealerName = (p.DEALERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.DEALERNAME)[0].ToString() + "-" + p.DEALERNAME : p.DEALERNAME,
                                               DealerID = p.DEALERID,
                                               DealerCode = p.DEALERCODE
                                           });
                        ddlProcessingDepartmentID.DataSource = list;
                        ddlProcessingDepartmentID.DataTextField = "DealerName";
                        ddlProcessingDepartmentID.DataValueField = "DealerID";
                        ddlProcessingDepartmentID.DataBind();
                        ddlProcessingDepartmentIDTwo.DataSource = list;
                        ddlProcessingDepartmentIDTwo.DataTextField = "DealerCode";
                        ddlProcessingDepartmentIDTwo.DataValueField = "DealerID";
                        ddlProcessingDepartmentIDTwo.DataBind();
                    }
                    break;
                default:
                    {

                    }
                    break;
            }

            ddlProcessingDepartmentID.Items.Insert(0, new ListItem("", ""));
            ddlProcessingDepartmentIDTwo.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 派工人
        /// DDLs the processing user ID binding.
        /// </summary>
        private void ddlProcessingUserIDBinding()
        {
            txtProcessingUserLogin.Text = "";
            txtProcessingUserName.Text = "";
            //string conditions = " and BUSINESSDIVISIONCODE = '{0}' and USERTYPE = '{1}' and DEPARTMENTID = '{2}' ";
            //string businessDivisionCode = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;

            //List<T_USER_Entity> userList = userBll.GetList(String.Format(conditions, businessDivisionCode, ddlProcessingObjectType.SelectedValue.Trim(), ddlProcessingDepartmentID.SelectedValue.Trim()), " USERNAME asc ");
            //var list = userList.Select(p => new
            //{
            //    UserName = (p.USERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.USERNAME)[0].ToString() + "-" + p.USERNAME : p.USERNAME,
            //    UserID = p.USERID,
            //    LoginName = p.LOGINNAME,
            //    PHONE = p.MOBILEPHONE + "(" + p.USERNAME + ")"
            //});


            //ddlProcessingUserID.DataSource = list;
            //ddlProcessingUserID.DataTextField = "PHONE";
            //ddlProcessingUserID.DataValueField = "UserID";
            //ddlProcessingUserID.DataBind();
            //ddlProcessingUserID.Items.Insert(0, new ListItem("", ""));
            //ddlProcessingUserIDTwo.DataSource = list;
            //ddlProcessingUserIDTwo.DataTextField = "LoginName";
            //ddlProcessingUserIDTwo.DataValueField = "UserID";
            //ddlProcessingUserIDTwo.DataBind();
            //ddlProcessingUserIDTwo.Items.Insert(0, new ListItem("", ""));

            //mddlProcessingUserID.DataSource = list;
            //mddlProcessingUserID.DataTextField = "UserName";
            //mddlProcessingUserID.DataValueField = "UserID";
            //mddlProcessingUserID.DataBind();


        }

        #endregion

        protected void ddlProcessingObjectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPROCESSINGMARKCODEBinding();
            ddlProcessingDepartmentIDBinding();
            ddlProcessingUserIDBinding();
        }
        
        protected void ddlPROCESSINGMARKCODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProcessingDepartmentIDBinding();
            ddlProcessingUserIDBinding();
        }

        protected void ddlProcessingDepartmentID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProcessingUserIDBinding();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string strUpdateContent = "";
            StringBuilder sbTemp = new StringBuilder();
            if (!(sender is Button)) return;
            Button btn = sender as Button;

            if (btn.ID == StaticConstant.PreRepairCase + "SAVE") //保存
            {
                string customerId = "";

                UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "RepairCase_btnOK_Click");
                HiddenField hfCustomerId = this.PlaceHolderCustomerInfo.FindControl("hfCustomerId") as HiddenField;
                if (hfCustomerId == null)
                {
                    PageHelper.ShowMessage(this.Page, "请选择客户！");
                    return;
                }
                customerId = hfCustomerId.Value;
                T_CUSTOMER_Entity customerEntity = customerBll.GetEntity("CustomerId", customerId);
                if (customerEntity == null)
                {
                    PageHelper.ShowMessage(this.Page, "请选择客户！");
                    return;
                }
                PageHelper.GetControlValue<T_CUSTOMER_Entity>(this.PlaceHolderCustomerInfo, ref customerEntity);
                customerBll.Update(customerEntity);
                ctBll.UpdateCustomerTelIndex(customerEntity);
                UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "RepairCase_btnOK_Click_CustomerUpdate");


                T_REPAIRCASE_Entity repairCaseEntity = new T_REPAIRCASE_Entity();
                repairCaseEntity.CASEID = repairCaseBll.GeneratorCaseID();
                strUpdateContent = PageHelper.GetControlValue<T_REPAIRCASE_Entity>(this.PlaceHolderCustomerInfo, ref repairCaseEntity);
                strUpdateContent += PageHelper.GetControlValue<T_REPAIRCASE_Entity>(this, ref repairCaseEntity);
                repairCaseEntity.BUSINESSDIVISIONCODE = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                repairCaseEntity.BUSINESSDIVISIONID = UserLoginInfo.UserEntity.BUSINESSDIVISIONID;
                repairCaseEntity.BUSINESSDIVISIONNAME = UserLoginInfo.UserEntity.BUSINESSDIVISIONNAME;

                try
                {
                    if (repairCaseEntity.PROCESSINGDEPARTMENTNAME.Trim() != ddlProcessingDepartmentID.SelectedItem.Text.Trim())
                    {
                        sbTemp.Append("PROCESSINGDEPARTMENTNAME:").Append(repairCaseEntity.PROCESSINGDEPARTMENTNAME).Append("->");
                        repairCaseEntity.PROCESSINGDEPARTMENTNAME = ddlProcessingDepartmentID.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.PROCESSINGDEPARTMENTNAME).Append("|");
                    }
                }
                catch
                {
                    
                }
                ddlProcessingDepartmentIDTwo.SelectedValue = ddlProcessingDepartmentID.SelectedValue;
                try
                {
                    if (repairCaseEntity.PROCESSINGDEPARTMENTCODE != ddlProcessingDepartmentIDTwo.SelectedItem.Text)
                    {
                        sbTemp.Append("PROCESSINGDEPARTMENTCODE:").Append(repairCaseEntity.PROCESSINGDEPARTMENTCODE).Append("->");
                        repairCaseEntity.PROCESSINGDEPARTMENTCODE = ddlProcessingDepartmentIDTwo.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.PROCESSINGDEPARTMENTCODE).Append("|");
                    }
                }
                catch
                {
                    
                }
                //ddlProcessingUserIDTwo.SelectedValue = ddlProcessingUserID.SelectedValue;
                //repairCaseEntity.PROCESSINGLOGINNAME = ddlProcessingUserIDTwo.SelectedItem.Text;
                //repairCaseEntity.PROCESSINGUSERNAME = ddlProcessingUserID.SelectedItem.Text;
                repairCaseEntity.CUSTOMERID = customerId;
                repairCaseEntity.CUSTOMERCODE = customerEntity.CUSTOMERCODE;
                repairCaseEntity.CALLED = PageHelper.GetQueryString(this.Page, "CALLED");
                repairCaseEntity.CALLER = PageHelper.GetQueryString(this.Page, "PHONE");
                repairCaseEntity.CALLID = PageHelper.GetQueryString(this.Page, "CALLID");
                repairCaseEntity.CALLTYPE = PageHelper.GetQueryString(this.Page, "CALLTYPE");
                repairCaseEntity.RECORDCODE = PageHelper.GetQueryString(this.Page, "RECORDCODE");
                repairCaseEntity.CREATEPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                repairCaseEntity.CREATEPEOPLEID = UserLoginInfo.UserEntity.USERID;
                repairCaseEntity.CREATETIME = DateTime.Now;
                repairCaseEntity.LASTMODIFYPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                repairCaseEntity.LASTMODIFYPEOPLEID = UserLoginInfo.UserEntity.USERID;
                repairCaseEntity.LASTMODIFYTIME = DateTime.Now;
                repairCaseEntity.HELPUSERID = UserLoginInfo.UserEntity.USERID;
                repairCaseEntity.HELPUSERNAME = UserLoginInfo.UserEntity.USERNAME;
                repairCaseEntity.CREATEDEPARTMENTID = UserLoginInfo.UserEntity.DEPARTMENTID;
                repairCaseEntity.CREATEDEPARTMENTNAME = UserLoginInfo.UserEntity.DEPARTMENTNAME;
                repairCaseEntity.CREATEUSERTYPE = UserLoginInfo.UserEntity.USERTYPE;
                

                try
                {
                    if (repairCaseEntity.DEALERNAME != ddlDEALERCODE.SelectedItem.Text)
                    {
                        sbTemp.Append("DEALERNAME:").Append(repairCaseEntity.DEALERNAME).Append("->");
                        repairCaseEntity.DEALERNAME = ddlDEALERCODE.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.DEALERNAME).Append("|");
                    }
                }
                catch
                {

                }
                try
                {
                    if (repairCaseEntity.STATIONNAME != ddlSTATIONCODE.SelectedItem.Text)
                    {
                        sbTemp.Append("STATIONNAME:").Append(repairCaseEntity.STATIONNAME).Append("->");
                        repairCaseEntity.STATIONNAME = ddlSTATIONCODE.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.STATIONNAME).Append("|");
                    }
                }
                catch
                {

                }


                if (txtSYMPTOM.ReadOnly)
                {
                    if (repairCaseEntity.SYMPTOM != txtSYMPTOM2.Text)
                    {
                        sbTemp.Append("SYMPTOM:").Append(repairCaseEntity.SYMPTOM).Append("->");
                        repairCaseEntity.SYMPTOM = txtSYMPTOM2.Text;
                        sbTemp.Append(repairCaseEntity.SYMPTOM).Append("|");
                    }

                    if (repairCaseEntity.FAILUREPARTS != txtFAILUREPARTS2.Text)
                    {
                        sbTemp.Append("FAILUREPARTS:").Append(repairCaseEntity.FAILUREPARTS).Append("->");
                        repairCaseEntity.FAILUREPARTS = txtFAILUREPARTS2.Text;
                        sbTemp.Append(repairCaseEntity.FAILUREPARTS).Append("|");
                    }

                    if (repairCaseEntity.FULLSYMPTOM != txtFULLSYMPTOM2.Text)
                    {
                        sbTemp.Append("FULLSYMPTOM:").Append(repairCaseEntity.FULLSYMPTOM).Append("->");
                        repairCaseEntity.FULLSYMPTOM = txtFULLSYMPTOM2.Text;
                        sbTemp.Append(repairCaseEntity.FULLSYMPTOM).Append("|");
                    }

                    txtSYMPTOM.Text = txtSYMPTOM2.Text;
                    txtFAILUREPARTS.Text = txtFAILUREPARTS2.Text;
                    txtFULLSYMPTOM.Text = txtFULLSYMPTOM2.Text;
                }

                try
                {
                    DropDownList ddl = (this.PlaceHolderCustomerInfo.FindControl("ddlCOUNTRY") as DropDownList);
                    if (repairCaseEntity.COUNTRY != ddl.SelectedItem.Text)
                    {
                        sbTemp.Append("COUNTRY:").Append(repairCaseEntity.COUNTRY).Append("->");
                        repairCaseEntity.COUNTRY = ddl.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.COUNTRY).Append("|");
                    }

                    ddl = (this.PlaceHolderCustomerInfo.FindControl("ddlPROVINCE") as DropDownList);
                    if (repairCaseEntity.PROVINCE != ddl.SelectedItem.Text)
                    {
                        sbTemp.Append("PROVINCE:").Append(repairCaseEntity.PROVINCE).Append("->");
                        repairCaseEntity.PROVINCE = ddl.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.PROVINCE).Append("|");
                    }
                    
                    ddl = (this.PlaceHolderCustomerInfo.FindControl("ddlPREFECTURECITY") as DropDownList);
                    if (repairCaseEntity.PREFECTURECITY != ddl.SelectedItem.Text)
                    {
                        sbTemp.Append("PREFECTURECITY:").Append(repairCaseEntity.PREFECTURECITY).Append("->");
                        repairCaseEntity.PREFECTURECITY = ddl.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.PREFECTURECITY).Append("|");
                    }

                    ddl = (this.PlaceHolderCustomerInfo.FindControl("ddlCITY") as DropDownList);
                    if (repairCaseEntity.CITY != ddl.SelectedItem.Text)
                    {
                        sbTemp.Append("CITY:").Append(repairCaseEntity.CITY).Append("->");
                        repairCaseEntity.CITY = ddl.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.CITY).Append("|");
                    }

                    ddl = (this.PlaceHolderCustomerInfo.FindControl("ddlVILLAGETOWN") as DropDownList);
                    if (repairCaseEntity.VILLAGETOWN != ddl.SelectedItem.Text)
                    {
                        sbTemp.Append("VILLAGETOWN:").Append(repairCaseEntity.VILLAGETOWN).Append("->");
                        repairCaseEntity.VILLAGETOWN = ddl.SelectedItem.Text;
                        sbTemp.Append(repairCaseEntity.VILLAGETOWN).Append("|");
                    }

                }
                catch
                {
                    sbTemp.Append("|");
                }

                #region 设定工单状态
                repairCaseEntity.STATUS = configBll.GetCacheValueByName(this.Page, "RepairCaseBeginStatus");
                //Button btn = sender as Button;
                //if (btn != null)
                //{
                //    switch (btn.CommandName)
                //    {
                //        case "RepairCaseDispatching":
                //            {
                //                repairCaseEntity.STATUS = configBll.GetCacheValueByName(this.Page,
                //                                                                        "RepairCaseDispatching");
                //                repairCaseEntity.DISPATCHINGTIME = DateTime.Now;
                //                if (StringHelper.IsNumber(ddlREMINDERTIME.SelectedValue.Trim()))
                //                {
                //                    repairCaseEntity.TODOREMINDERTIME =
                //                        DateTime.Now.AddHours(StringHelper.ToInt(ddlREMINDERTIME.SelectedValue.Trim(), 0));
                //                }
                //            }
                //            break;
                //        case "RepairCaseCompleted":
                //            {
                //                repairCaseEntity.STATUS = configBll.GetCacheValueByName(this.Page, "RepairCaseCompleted");
                //            }
                //            break;
                //    }
                //}

                #endregion

                if (String.IsNullOrEmpty(repairCaseEntity.ADDRESS))
                {
                    StringBuilder sbAddress = new StringBuilder();
                    sbAddress.Append(repairCaseEntity.COUNTRY).Append("-");
                    sbAddress.Append(repairCaseEntity.PROVINCE).Append("-");
                    sbAddress.Append(repairCaseEntity.PREFECTURECITY).Append("-");
                    sbAddress.Append(repairCaseEntity.CITY).Append("-");
                    sbAddress.Append(repairCaseEntity.VILLAGETOWN).Append("-");
                    sbAddress.Append(repairCaseEntity.VILLAGE).Append("-");
                    sbAddress.Append(repairCaseEntity.GROUPTEAM);
                    if (repairCaseEntity.ADDRESS.Trim() != sbAddress.ToString().Trim())
                    {
                        sbTemp.Append("ADDRESS:").Append(repairCaseEntity.ADDRESS).Append("->");
                        repairCaseEntity.ADDRESS = sbAddress.ToString();
                        sbTemp.Append(repairCaseEntity.ADDRESS).Append("|");
                    }
                }

                repairCaseEntity.VEHICLEMARKNAME = marketDepartmentBll.GetMarketDepartmentName(this.Page, repairCaseEntity.VEHICLEMARKCODE.Trim(), UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim());

                UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "RepairCase_btnOK_Click_CaseBegin");
                long rel = repairCaseBll.Insert(repairCaseEntity);
                RepairCaseStatus = repairCaseEntity.STATUS;
                lblRepairCaseID.Text = repairCaseEntity.CASEID.ToString();
                if (rel > 0)
                {
                   
                    #region 设置关联车辆

                    UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "RepairCase_btnOK_Click_VehicleRelationBegin");
                    repairCaseBll.SetVehicleRelation(repairCaseEntity, UserLoginInfo);

                    #endregion

                    #region 录音

                    //调用线程去处理获取工单录音信息
                    //RecordWrite(repairCaseEntity);
                    recordBll.XmlUrl = configBll.GetCacheValueByName(this.Page, "WFOSearch");
                    recordBll.WfoUserName = configBll.GetCacheValueByName(this.Page, "WFOUserName");
                    recordBll.WfoPassword = configBll.GetCacheValueByName(this.Page, "WFOPassword");
                    recordBll.UserLoginInfo = UserLoginInfo;
                    System.Threading.Thread SyncData = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(recordBll.RepairCaseRecordWrite));
                    SyncData.Start(repairCaseEntity);



                    #endregion

                    #region 状态变更日志

                    if (configBll.GetCacheValueByName(this.Page, "WorkOrderLog").ToLower() != "False")
                    {
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "RepairCase_btnOK_Click_workOrderLogBegin");
                        T_WORKORDERLOG_Entity workOrderLogEntity = new T_WORKORDERLOG_Entity();
                        workOrderLogEntity.CASEID = repairCaseEntity.CASEID;
                        workOrderLogEntity.CUSTOMERCODE = repairCaseEntity.CUSTOMERCODE;
                        workOrderLogEntity.CREATEPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                        workOrderLogEntity.CREATEPEOPLEID = UserLoginInfo.UserEntity.USERID;
                        workOrderLogEntity.CREATETIME = DateTime.Now;
                        workOrderLogEntity.ORDERTYPEID = (int) WorkOrderType.报修工单;
                        workOrderLogEntity.ORDERTYPENAME = (WorkOrderType.报修工单).ToString();
                        workOrderLogEntity.OPERATIONNAME = btn.Text;
                        workOrderLogEntity.UPDATECONTENT = strUpdateContent + sbTemp.ToString();
                        workOrderLogEntity.NEXTSTATUSID = StringHelper.ToInt(repairCaseEntity.STATUS, 0);
                        try
                        {
                            workOrderLogEntity.NEXTSTATUSNAME =
                                workOrderStatusBll.GetPageItem_T_WORKORDERSTATUS_EntityByKey(this.Page,
                                                                                             StringHelper.ToInt(
                                                                                                 repairCaseEntity.STATUS,
                                                                                                 0))
                                    .ORDERSTATUSNAME;
                        }
                        catch (Exception)
                        {

                        }
                        workOrderLogBll.Insert(workOrderLogEntity);
                    }
                    UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + UserLoginInfo.UserEntity.USERID.ToString(), "RepairCase_btnOK_Click_End");

                    #endregion

                    LoadButton();

                    PageHelper.ShowMessage(this.Page, "工单保存成功！");
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, "工单保存失败！");
                }
            }
            else
            {
                string operationId = btn.CommandName.Replace(StaticConstant.PreRepairCase, "");
                T_WORKORDEROPERATION_Entity workOrderOperationEntity =
                    workOrderOperationBll.GetCache_T_WORKORDEROPERATION_EntityByKey(this.Page, StringHelper.ToInt(operationId, 0));

                string _caseid = lblRepairCaseID.Text.Trim();
                T_REPAIRCASE_Entity repaircaseEntity = repairCaseBll.GetEntity("CASEID", _caseid);

                if (repaircaseEntity == null)
                {
                    return;
                }

                if ((IsUpdateInfo)Enum.Parse(typeof(IsUpdateInfo), workOrderOperationEntity.ISUPDATEINFO.ToString()) == IsUpdateInfo.是)
                {
                    strUpdateContent = PageHelper.GetControlValue<T_REPAIRCASE_Entity>(this, ref repaircaseEntity);

                    try
                    {
                        if (repaircaseEntity.PROCESSINGDEPARTMENTNAME.Trim() != ddlProcessingDepartmentID.SelectedItem.Text.Trim())
                        {
                            sbTemp.Append("PROCESSINGDEPARTMENTNAME:").Append(repaircaseEntity.PROCESSINGDEPARTMENTNAME).Append("->");
                            repaircaseEntity.PROCESSINGDEPARTMENTNAME = ddlProcessingDepartmentID.SelectedItem.Text;
                            sbTemp.Append(repaircaseEntity.PROCESSINGDEPARTMENTNAME).Append("|");
                        }
                    }
                    catch { }
                    try
                    {
                        ddlProcessingDepartmentIDTwo.SelectedValue = ddlProcessingDepartmentID.SelectedValue;
                    }
                    catch { }
                    try
                    {
                        if (repaircaseEntity.PROCESSINGDEPARTMENTCODE != ddlProcessingDepartmentIDTwo.SelectedItem.Text)
                        {
                            sbTemp.Append("PROCESSINGDEPARTMENTCODE:").Append(repaircaseEntity.PROCESSINGDEPARTMENTCODE).Append("->");
                            repaircaseEntity.PROCESSINGDEPARTMENTCODE = ddlProcessingDepartmentIDTwo.SelectedItem.Text;
                            sbTemp.Append(repaircaseEntity.PROCESSINGDEPARTMENTCODE).Append("|");
                        }
                    }
                    catch
                    {

                    }
                    if (txtSYMPTOM.ReadOnly)
                    {
                        if (repaircaseEntity.SYMPTOM != txtSYMPTOM2.Text)
                        {
                            sbTemp.Append("SYMPTOM:").Append(repaircaseEntity.SYMPTOM).Append("->");
                            repaircaseEntity.SYMPTOM = txtSYMPTOM2.Text;
                            sbTemp.Append(repaircaseEntity.SYMPTOM).Append("|");
                        }

                        if (repaircaseEntity.FAILUREPARTS != txtFAILUREPARTS2.Text)
                        {
                            sbTemp.Append("FAILUREPARTS:").Append(repaircaseEntity.FAILUREPARTS).Append("->");
                            repaircaseEntity.FAILUREPARTS = txtFAILUREPARTS2.Text;
                            sbTemp.Append(repaircaseEntity.FAILUREPARTS).Append("|");
                        }

                        if (repaircaseEntity.FULLSYMPTOM != txtFULLSYMPTOM2.Text)
                        {
                            sbTemp.Append("FULLSYMPTOM:").Append(repaircaseEntity.FULLSYMPTOM).Append("->");
                            repaircaseEntity.FULLSYMPTOM = txtFULLSYMPTOM2.Text;
                            sbTemp.Append(repaircaseEntity.FULLSYMPTOM).Append("|");
                        }


                        txtSYMPTOM.Text = txtSYMPTOM2.Text;
                        txtFAILUREPARTS.Text = txtFAILUREPARTS2.Text;
                        txtFULLSYMPTOM.Text = txtFULLSYMPTOM2.Text;
                    }

                    //ddlPROCESSINGDEPARTMENTIDTwo.SelectedValue = ddlPROCESSINGDEPARTMENTID.SelectedValue;
                    //repaircaseEntity.PROCESSINGDEPARTMENTCODE = ddlPROCESSINGDEPARTMENTIDTwo.SelectedItem.Text;
                    //ddlPROCESSINGUSERIDTwo.SelectedValue = ddlPROCESSINGUSERID.SelectedValue;
                    //repaircaseEntity.PROCESSINGLOGINNAME = ddlPROCESSINGUSERIDTwo.SelectedItem.Text;

                    if (ddlDEALERCODE.SelectedItem != null && repaircaseEntity.DEALERNAME != ddlDEALERCODE.SelectedItem.Text)
                    {
                        sbTemp.Append("DEALERNAME:").Append(repaircaseEntity.DEALERNAME).Append("->");
                        repaircaseEntity.DEALERNAME = (ddlDEALERCODE.SelectedItem == null) ? "" : ddlDEALERCODE.SelectedItem.Text;
                        sbTemp.Append(repaircaseEntity.DEALERNAME).Append("|");
                    }

                    if (ddlSTATIONCODE.SelectedItem != null && repaircaseEntity.STATIONNAME != ddlSTATIONCODE.SelectedItem.Text)
                    {
                        sbTemp.Append("STATIONNAME:").Append(repaircaseEntity.STATIONNAME).Append("->");
                        repaircaseEntity.STATIONNAME = (ddlSTATIONCODE.SelectedItem == null) ? "" : ddlSTATIONCODE.SelectedItem.Text;
                        sbTemp.Append(repaircaseEntity.STATIONNAME).Append("|");
                    }

                }

                //执行相关反射操作
                MethodResult result = new MethodResult();
                workOrderOperationBll.AssociatedAction<T_REPAIRCASE_Entity>(workOrderOperationEntity, repaircaseEntity, result);
                if(result.Result == MethodResult.ResultType.Failure)
                {
                    PageHelper.ShowMessage(this.Page, result.Message);
                    return; 
                }

                repaircaseEntity.STATUS = workOrderOperationEntity.NEXTSTATUSID.ToString();
                //sbTemp.Append("STATUS:").Append(repaircaseEntity.STATUS).Append("|");

                if (repaircaseEntity.DISPATCHINGTIME.HasValue)
                {
                    if (StringHelper.IsNumber(ddlREMINDERTIME.SelectedValue.Trim()))
                    {
                        repaircaseEntity.TODOREMINDERTIME = repaircaseEntity.DISPATCHINGTIME.Value.AddHours(StringHelper.ToInt(ddlREMINDERTIME.SelectedValue.Trim(), 0));
                    }
                }
                repaircaseEntity.LASTMODIFYPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                repaircaseEntity.LASTMODIFYPEOPLEID = UserLoginInfo.UserEntity.USERID;
                repaircaseEntity.LASTMODIFYTIME = DateTime.Now;

                repaircaseEntity.VEHICLEMARKNAME = marketDepartmentBll.GetMarketDepartmentName(this.Page, repaircaseEntity.VEHICLEMARKCODE.Trim(), UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim());

                long rel = repairCaseBll.Update(repaircaseEntity);
                RepairCaseStatus = repaircaseEntity.STATUS;
                if (rel > 0)
                {

                    #region 更新到中间表
                    if (workOrderStatusBll.GetSessionStatusType(this.Page, repaircaseEntity.STATUS) == StatusType.结束节点)
                    {
                        try
                        {
                            CC_REPAIRCASE_BLL ccBll = new CC_REPAIRCASE_BLL();
                            CC_REPAIRCASE_Entity ccEntity = new CC_REPAIRCASE_Entity();
                            ccBll._dal.ObjectCopyTo(repaircaseEntity, ccEntity);
                            ccBll.Delete("CASEID", repaircaseEntity.CASEID.ToString());
                            ccBll.Insert(ccEntity);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.WriteLog("MiddleDataBase", String.Format("CaseID:{0}--Message:{1}", repaircaseEntity.CASEID.ToString(), ex.Message));

                        }
                    } 
                    #endregion
                    #region 状态日志
                    try
                    {
                        if (configBll.GetCacheValueByName(this.Page, "WorkOrderLog").ToLower() != "False")
                        {
                            T_WORKORDERLOG_Entity workOrderLogEntity = new T_WORKORDERLOG_Entity();
                            workOrderLogBll._dal.ObjectCopyTo(workOrderOperationEntity, workOrderLogEntity);
                            workOrderLogEntity.CASEID = repaircaseEntity.CASEID;
                            workOrderLogEntity.CUSTOMERCODE = repaircaseEntity.CUSTOMERCODE;
                            workOrderLogEntity.CREATEPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                            workOrderLogEntity.CREATEPEOPLEID = UserLoginInfo.UserEntity.USERID;
                            workOrderLogEntity.CREATETIME = DateTime.Now;
                            workOrderLogEntity.UPDATECONTENT = strUpdateContent + sbTemp.ToString();
                            workOrderLogBll.Insert(workOrderLogEntity);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    #endregion

                    LoadButton();
                    //string backUrl = UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this.Page));
                    PageHelper.ShowMessage(this.Page, String.Format("执行操作 {0} 成功！工单状态由 {1} 更新为 {2} ！", workOrderOperationEntity.OPERATIONNAME, workOrderOperationEntity.CURRENTSTATUSNAME, workOrderOperationEntity.NEXTSTATUSNAME));
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, String.Format("执行操作 {0} 失败！", workOrderOperationEntity.OPERATIONNAME));
                }

            }


        }

        /// <summary>
        /// Records the write.
        /// </summary>
        /// <param name="repairCaseEntity">The repair case entity.</param>
        private void RecordWrite(T_REPAIRCASE_Entity repairCaseEntity)
        {
            try
            {
                T_RECORD_Entity recordEntity = recordBll.GetRemoteInfo(repairCaseEntity.CALLID);
                recordEntity.CALLTYPE = repairCaseEntity.CALLTYPE;
                recordEntity.CASEID = repairCaseEntity.CASEID;
                recordEntity.CALLER = repairCaseEntity.CALLER;
                recordEntity.CALLED = repairCaseEntity.CALLED;
                recordEntity.CASETYPE = (int)WorkOrderType.报修工单;
                recordEntity.CREATEPEOPLE = UserLoginInfo.UserEntity.USERNAME;
                recordEntity.CREATEPEOPLEID = UserLoginInfo.UserEntity.USERID;
                recordEntity.CREATETIME = DateTime.Now;
                recordEntity.BUSINESSDIVISIONCODE = UserLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                recordEntity.BUSINESSDIVISIONID = UserLoginInfo.UserEntity.BUSINESSDIVISIONID;
                recordEntity.BUSINESSDIVISIONNAME = UserLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                recordBll.Insert(recordEntity);
            }
            catch (Exception ex)
            {

            }
        }



        

    }
}